import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_rider/event/click_close_stations_event.dart';
import 'package:flutter_rider/generated/r.dart';
import 'package:flutter_rider/main/widgets/ext/u_text.dart';
import 'package:flutter_rider/main/widgets/pop/pop_car_list.dart';
import 'package:flutter_rider/main/widgets/ui_constant.dart';
import 'package:flutter_rider/main/models/station_model.dart';
import 'package:flutter_rider/utils/open_utils.dart';
import 'package:flutter_rider/utils/screen_fit.dart';
import 'package:flutter_rider/main.dart';
import 'package:flutter_svg/svg.dart';

import '../../../event/change_stations_event.dart';

class PopStations extends StatefulWidget {

  StationModel stationModel;

  PopStations(this.stationModel, {Key? key}) : super(key: key);
  
  @override
  State createState() => _PopStationsState();
}

class _PopStationsState extends State<PopStations> {
  
  late StreamSubscription _eventFn;

  @override
  void initState() {
    super.initState();
    _eventFn = eventBus.on<ClickCloseStationsEvent>().listen((event) {
      Navigator.of(context).pop();
    });
    // widget.stationModel.alias.add(Alias(1, "hahha1"));
    // widget.stationModel.alias.add(Alias(1, "hahha2"));
    // widget.stationModel.alias.add(Alias(1, "hahha3"));
  }

  @override
  void dispose() {
    super.dispose();
    _eventFn.cancel();
  }

  @override
  Widget build(BuildContext context) {

    int length = widget.stationModel.alias.length;
    double height =  (44 * length).rpx;

    return 
    IntrinsicHeight(child: 
    Container(
      // height: 273.rpx + MediaQuery.of(context).padding.bottom,
      padding: EdgeInsets.only(left: MediaQuery.of(context).padding.left, right: MediaQuery.of(context).padding.right,bottom: MediaQuery.of(context).padding.bottom),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.vertical(top: Radius.circular(8.rpx)),
      ),
      child: Column(
        children: [
          8.hspace,
          Container(
            width: 36.rpx,
            height: 5.rpx,
            decoration: BoxDecoration(
              color: Colors.black.withOpacity(0.26),
              borderRadius: BorderRadius.circular(2.rpx),
            ),
          ),
          Container(
              height: 52.rpx,
              alignment: Alignment.center,
              child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    16.wspace,
                    24.wspace,
                    Expanded(
                        child: Container(
                      alignment: Alignment.center,
                      child: UText.get("站点信息", Colors.black, 20.rpx,
                          fw: FontWeight.bold),
                    )),
                    4.wspace,
                    InkWell(child: 
                    SvgPicture.asset(R.image_close,
                        width: 24.rpx, height: 24.rpx),
                      onTap: _closeClick,
                    ),
                    16.wspace,
                  ])),
            Container(
              alignment: Alignment.centerLeft,
              padding:EdgeInsets.only(left: 24.rpx, right:24.rpx,bottom: 0.rpx,top: 0.rpx),
              // height: 0.5.rpx,
              child: UText.get(widget.stationModel.station_name, Colors.black, 16.rpx),
            ),
            Container(
              // padding:EdgeInsets.all(0),
              height: height,
              child: ListView.builder(
            itemCount: length,
            itemBuilder:((context, index) => _itemBuilder(widget.stationModel.alias[index])), 
            ),
            ),

           Container(
            child: Padding(
                padding: EdgeInsets.only(left: 16.rpx, right: 16.rpx,bottom: 10.rpx,top: 32.rpx),
                child: MaterialButton(
                  minWidth: double.infinity,
                  height: 40.rpx,
                  onPressed:  () {
                    _onSubmitClick();
                  },
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(4.rpx),
                  ),
                  color: UIColor.color_primary,
                  disabledColor: Colors.black.withOpacity(0.26),
                  child: UText.get("去调车", Colors.white, 14.rpx),
                )),
          )
        ],
      ),
      )
    );
  }

  Widget _itemBuilder(Alias alias) => Container(
              height: 44.rpx,
              alignment: Alignment.centerLeft,
              padding:EdgeInsets.only(left: 24.rpx, right:24.rpx,top: 12.rpx),
              // height: 0.5.rpx,
              child: UText.get(alias.name, UIColor.color_t3, 14.rpx),
            );

  _onSubmitClick() {
    print("去调车");
    OpenUtils.pop2(context,PopCarList(widget.stationModel,true));
  }

 
   _closeClick() {
    print("关闭");

    eventBus.fire(ChangeStationsEvent());
  }

}